package com.generalbackend.role.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.generalbackend.role.mapper.PermissionRestApiMapper;
import com.generalbackend.role.model.entity.PermissionRestApi;
import com.generalbackend.role.model.query.PermissionRestApiQuery;
import com.generalbackend.role.service.IPermissionRestApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.generalbackend.common.constants.Constants.DATA;
import static com.generalbackend.common.constants.Constants.TOTAL;

/**
* @Description 接口权限表业务层实现类
* @author tim.xie
* @date 2021-12-14 13:26
*/

@Service
public class PermissionRestApiServiceImpl extends ServiceImpl<PermissionRestApiMapper, PermissionRestApi> implements IPermissionRestApiService {

	@Autowired
	private PermissionRestApiMapper permissionRestApiMapper;

	@Override
	public Boolean insert(PermissionRestApi permissionRestApi) {
        // 新增校验
        checkPermissionRestApi(permissionRestApi);
        // 填充额外属性
        fillPermissionRestApi(permissionRestApi);
        Integer num = permissionRestApiMapper.insert(permissionRestApi);
        if(num <= 0) {
         return false;
         }
		return true;
	}

	@Override
	public Boolean update(PermissionRestApi permissionRestApi) {
        // 修改校验
        checkPermissionRestApi(permissionRestApi);
        // 填充额外属性
        fillPermissionRestApi(permissionRestApi);
        Integer num = permissionRestApiMapper.updateById(permissionRestApi);
        if(num <= 0) {
          return false;
        }
		return true;
	}
    /**
    * 新增或者修改校验
    * @param permissionRestApi
    * @return
    */
    private void checkPermissionRestApi(PermissionRestApi permissionRestApi) {

    }
    /**
    *填充额外属性
    **/
    private void fillPermissionRestApi(PermissionRestApi permissionRestApi) {

    }

	@Override
	public Boolean deleteById(String id) {
        permissionRestApiMapper.deleteById(id);
		return true;
	}

	@Override
	public PermissionRestApi selectById(String id) {
        PermissionRestApi permissionRestApi = permissionRestApiMapper.selectById(id);
        return permissionRestApi;
	}
    @Override
    public JSONObject list(PermissionRestApiQuery permissionRestApiQuery) {
        JSONObject result = new JSONObject();
        Page<PermissionRestApi> page = new Page<>(permissionRestApiQuery.getOffset(), permissionRestApiQuery.getLimit());
        List<PermissionRestApi> permissionRestApiList = permissionRestApiMapper.list(page,permissionRestApiQuery);
        result.put(TOTAL,  page.getTotal());
        result.put(DATA, permissionRestApiList);
        return result;
    }



}
